let request = require('../../utils/request.js')
let app = getApp();
Page({
  data: {
    showd: false,
    list: [
      { id: 0, cash: 10 }, { id: 1, cash: 30 }, { id: 2, cash: 50 }, { id: 3, cash: 100 }
    ],
    selected:-1,
    carddata: '',
    cardId: '',
    cardInfo:{
      balance: '',//余额
      telephone:'',
      username:''
    },
    elseCash:'',//其他金额
    listCash:'',//列表金额
    postList: {},
    areaId: ''
  },
  onLoad(data) {
    this.data.carddata = data
  },
  onShow() {
    my.showLoading({
      title: '加载中'
    })
    my.getNetworkType({
      success: (res) => {
        if (!res.networkAvailable || res.networkType == 'UNKNOWN') {
          my.showToast({
            content: '网络状态不佳，请检查网络',
            type: 'fail',
            duration: 2000
          })
        }
      }
    })
    if (!!this.data.carddata && this.data.carddata.q) {
      this.data.cardId = this.data.carddata.q.substr(this.data.carddata.q.length - 8, 8)
    } else {
      this.data.cardId = this.data.carddata.cardId
    }
    this.isbindUser();
  },
  // 充值协议
  activity_click() {
    this.setData({
      showd: !this.data.showd
    })
  },
  // 判断是否绑定用户
  isbindUser() {
    let success = (res) => {
      my.hideLoading()
      if (res.isExist) {
        this.data.cardInfo.username = res.cardInfo.userName
        this.data.cardInfo.telephone = res.telephone
        this.data.cardInfo.balance = res.cardInfo.balance == null ? 0 : res.cardInfo.balance
        this.setData({
          cardInfo: this.data.cardInfo,
          cardId: this.data.cardId
        })
      } else {
        my.navigateTo({
          url: '/pages/scanRecharge/mobile?cardId='+ this.data.cardId
        })
      }
    }
    request.getdata("/user/isExist", {cardId:this.data.cardId}, success, this);
  },
  //获取选中金额
  choose(e) {
    this.data.select= e.currentTarget.dataset.item.id
    this.data.listCash = e.currentTarget.dataset.item.cash
    this.setData({
      select: this.data.select
    })
  },
  //其他金额
  elseChoose(e) {
    this.data.elseCash = e.detail.value
    this.setData({
      elseCash: this.data.elseCash
    });
  },
  pay() {
    my.showLoading({
      title: '加载中'
    })
    this.data.postList['cardId'] = this.data.cardId
    this.data.postList['balance'] = this.data.cardInfo.balance
    if (this.data.elseCash){
      this.data.postList.cash = this.data.elseCash
    }else{
      this.data.postList.cash = this.data.listCash
    }
    this.data.postList['source'] = 1
    let success = (param) => {
      my.tradePay({
        orderStr: param.callback,
        success: (res) => {
          setTimeout(() => {
            my.hideLoading();
          }, 2000)
          if (res.resultCode == 9000) {
            this.isbindUser()
            my.showToast({
              content: '支付成功',
              type: 'success',
              duration: 1000
            })
          }
        },
        fail: (res) => {
          setTimeout(() => {
            my.hideLoading();
          }, 2000)
        }
      })
    }
    request.postdata('/card/recharge', this.data.postList, success, this)
  }
})